package com.teamcenter.rac.massupdate.handlers;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.handlers.HandlerUtil;

import com.teamcenter.rac.aif.AbstractAIFOperation;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentChangeItemRevision;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.massupdate.model.MassUpdateChangeObjectValidationModel;
import com.teamcenter.rac.massupdate.ui.Messages;
import com.teamcenter.rac.massupdate.utils.MassUpdateHelper;
import com.teamcenter.rac.massupdate.utils.MassUpdateSupportOperation;
import com.teamcenter.rac.util.AdapterUtil;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PlatformHelper;
@SuppressWarnings({"unused","rawtypes","unchecked"})
public class MassUpdateHandler extends AbstractMassUpdateHandler {
	public static final String MASS_UPDATE_PROCESS_ID = "massupdate";
	private static MassUpdateSupportOperation m_massUpdateOperationSupport;

	public Object execute(ExecutionEvent paramExecutionEvent) throws ExecutionException {
		TCComponentItemRevision localTCComponentItemRevision = null;
		ISelection localISelection = HandlerUtil.getCurrentSelection(paramExecutionEvent);
		if ((localISelection != null) && (!localISelection.isEmpty())
				&& ((localISelection instanceof IStructuredSelection))) {
			StructuredSelection localObject = (StructuredSelection) localISelection;
			localTCComponentItemRevision = (TCComponentItemRevision) AdapterUtil
					.getAdapter(((StructuredSelection) localObject).getFirstElement(), TCComponentItemRevision.class);
		}
		final Object localObject = localTCComponentItemRevision;
		text(localTCComponentItemRevision);
		AbstractAIFOperation local1 = new AbstractAIFOperation() {
			public void executeOperation() throws Exception {
				Map localMap = MassUpdateHandler.this.createParameters((TCComponentItemRevision) localObject);
				if (localMap == null) {
					return;
				}
				String str = "com.teamcenter.rac.massupdate.openWizard";
				if (localMap.get("com.teamcenter.rac.massupdate.markup") != null) {
					str = "com.teamcenter.rac.massupdate.openManageMarkup";
					localMap.remove("com.teamcenter.rac.massupdate.markup");
				}
				MassUpdateHandler.this.callHandler(str, localMap);
			}
		};
		TCSession localTCSession = getTCSession();
		if (localTCSession != null) {
			localTCSession.queueOperation(local1);
		}
		return null;
	}

	private void text(TCComponentItemRevision rev)  {
		if (null == rev)
			return;
		try {
			TCComponent comps=rev.getRelatedComponent("pseudo_folder");
			System.out.println();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	protected Map<String, String> createParameters(TCComponentItemRevision paramTCComponentItemRevision)
			throws TCException {
		HashMap localHashMap = new HashMap();
		if (paramTCComponentItemRevision != null) {
			boolean bool = MassUpdateHelper.isChangeItemRevision(paramTCComponentItemRevision);
			if (!bool) {
				localHashMap.put("com.teamcenter.rac.massupdate.targetPart", paramTCComponentItemRevision.getUid());
			} else {
				localHashMap.put("com.teamcenter.rac.massupdate.changeObject", paramTCComponentItemRevision.getUid());
				paramTCComponentItemRevision.clearCache("IMAN_reference");
				TCComponent localTCComponent = MassUpdateHelper.getMarkupObject(paramTCComponentItemRevision);
				if (localTCComponent != null) {
					String str1 = getUpdateJobCommandId();
					final MassUpdateChangeObjectValidationModel localMassUpdateChangeObjectValidationModel = new MassUpdateChangeObjectValidationModel(
							str1, new TCComponentChangeItemRevision[] {
									(TCComponentChangeItemRevision) paramTCComponentItemRevision });
					MassUpdateSupportOperation localMassUpdateSupportOperation = getOperationSupport();
					localMassUpdateSupportOperation.validateChangeObjects(localMassUpdateChangeObjectValidationModel,
							getTCSession());
					if (localMassUpdateChangeObjectValidationModel.getErrorMessages() != null) {
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								MassUpdateHelper.displayMessage(
										Arrays.asList(localMassUpdateChangeObjectValidationModel.getErrorMessages()),
										Messages.ErrorDialog_Title, null);
							}
						});
						return null;
					}
					if (!localMassUpdateChangeObjectValidationModel
							.getChangeItemRevisionValidity((TCComponentChangeItemRevision) paramTCComponentItemRevision)
							.booleanValue()) {
						final String str2 = paramTCComponentItemRevision.getProperty("object_string");
						Display.getDefault().asyncExec(new Runnable() {
							public void run() {
								MessageBox.post(PlatformHelper.getCurrentShell(),
										NLS.bind(Messages.MassUpdate_ChangeObject_Validation_Failed, str2),
										Messages.ErrorDialog_Title, 1);
							}
						});
						return null;
					}
					localHashMap.put("com.teamcenter.rac.massupdate.markup", localTCComponent.getUid());
				}
			}
		}
		return localHashMap;
	}

	public String getUpdateJobCommandId() {
		return "massupdate";
	}

	protected MassUpdateSupportOperation getOperationSupport() {
		if (m_massUpdateOperationSupport == null) {
			m_massUpdateOperationSupport = new MassUpdateSupportOperation();
		}
		return m_massUpdateOperationSupport;
	}
}
